package com.elinkway.infinitemovies.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.elinkway.infinitemovies.async.MoviesHttpAsyncTask;
import com.elinkway.infinitemovies.bean.UserUpdateBean;
import com.elinkway.infinitemovies.http.api.MoviesHttpApi;
import com.elinkway.infinitemovies.http.parser.UserUpdateParser;
import com.le123.ysdq.R;
import com.lvideo.http.bean.LVideoDataHull;
import com.umeng.analytics.MobclickAgent;

public class SexModifyActivity extends BaseSecondaryActivity implements OnClickListener{
    public static final String TAG = "SexModifyActivity";
    public final static String GENDER = "gender";
    private RelativeLayout mBoyContainer;
    private RelativeLayout mGirlContainer;
    private RelativeLayout mSecretContainer;
    private ImageView mBoySelect;
    private ImageView mGirlSelect;
    private ImageView mSecretSelect;
    private String mCurrentSexSelect;
    private String mCurrentGender;
    private RequestUserUpdateTask mRequestUserUpdateTask;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setActionBarActivityClassName(TAG);
        setContentView(R.layout.activity_sex_modify);
        initActionBar();
        mTitleTv.setText(getResources().getString(R.string.sex));
        initView();
        setListener();
    }

    private void initView() {
        mBoyContainer = (RelativeLayout) findViewById(R.id.boyContainer);
        mGirlContainer = (RelativeLayout) findViewById(R.id.girlContainer);
        mSecretContainer = (RelativeLayout) findViewById(R.id.secretContainer);
        mBoySelect = (ImageView) findViewById(R.id.boy_select);
        mGirlSelect = (ImageView) findViewById(R.id.girl_select);
        mSecretSelect = (ImageView) findViewById(R.id.secret_select);
        mRightFirstTv.setText(getResources().getString(R.string.ok));
        mRightFirstTv.setVisibility(View.VISIBLE);
        mRightFirstTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                mRequestUserUpdateTask = new RequestUserUpdateTask(SexModifyActivity.this, mCurrentGender);
                mRequestUserUpdateTask.start();
            }
        });
    }

    private void setListener() {
        mBoyContainer.setOnClickListener(this);
        mGirlContainer.setOnClickListener(this);
        mSecretContainer.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        int gender = Integer.valueOf("".equals(getIntent().getStringExtra(GENDER)) ? "2" : getIntent().getStringExtra(GENDER));
        genderShow(gender);
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.boyContainer:
                genderShow(0);
                break;
            case R.id.girlContainer:
                genderShow(1);
                break;
            case R.id.secretContainer:
                genderShow(2);
                break;
        }
    }


    public void genderShow(int gender) {
        if(0 == gender) {
            mBoySelect.setVisibility(View.VISIBLE);
            mGirlSelect.setVisibility(View.GONE);
            mSecretSelect.setVisibility(View.GONE);
            mCurrentSexSelect = "男";
            mCurrentGender = "0";
        }else if(1 == gender) {
            mBoySelect.setVisibility(View.GONE);
            mGirlSelect.setVisibility(View.VISIBLE);
            mSecretSelect.setVisibility(View.GONE);
            mCurrentSexSelect = "女";
            mCurrentGender = "1";
        }else if(2 == gender) {
            mBoySelect.setVisibility(View.GONE);
            mGirlSelect.setVisibility(View.GONE);
            mSecretSelect.setVisibility(View.VISIBLE);
            mCurrentSexSelect = "保密";
            mCurrentGender = "2";
        }
    }

    public class RequestUserUpdateTask extends MoviesHttpAsyncTask<UserUpdateBean> {

        private String gender;

        @Override
        public void netNull() {
            super.netNull();
        }

        @Override
        public void netErr(int updateId, String errMsg) {
            super.netErr(updateId, errMsg);
        }

        @Override
        public void dataNull(int updateId, String errMsg) {
            super.dataNull(updateId, errMsg);
        }

        public RequestUserUpdateTask(Context context, String gender) {
            super(context);
            this.gender = gender;
        }

        @Override
        public LVideoDataHull<UserUpdateBean> doInBackground() {
            LVideoDataHull<UserUpdateBean> ret = MoviesHttpApi.userUpdateRequest(
                    new UserUpdateParser(), "", gender, "");
            if (ret.getDataType() == LVideoDataHull.DataType.DATA_IS_INTEGRITY) {
                UserUpdateBean v = ret.getDataEntity();

            }
            return ret;
        }

        @Override
        public void onPostExecute(int updateId, UserUpdateBean result) {
            Intent intent = new Intent();
            intent.putExtra(UserMessageActivity.SEX, mCurrentSexSelect);
            SexModifyActivity.this.setResult(RESULT_OK, intent);
            finish();
        }
    }
}
